# -*- coding: utf-8 -*-
# @Time : 2022/5/26 9:49
# @Author : WangYunfei
# @FileName: demo2.py
# @Email: yunfei.wang@innvote.com
import requests
from lxml import etree


def get_dom_list(url):
    req = requests.get(url)
    if req.status_code == 200:
        html = etree.HTML(req.text)
        dom_list = html.xpath('//*[@id="root"]/div/div[4]/div[2]/div[2]/ul/li')
        return dom_list
    else:
        print("请求链接失败 ！")
        return None


if __name__ == '__main__':
    _url = "https://finance.ifeng.com/gold/"

    text_util = lambda x: x[0] if x else ""

    for item in get_dom_list(_url):
        title = text_util(item.xpath("./div//a/@title"))
        time_str = text_util(item.xpath(".//time/text()"))
        print(f"时间：{time_str}, 标题：{title}")
